<?php
	if (isset($_GET['task'])) $task = $_GET['task']; else $task = '';
	if (isset($_GET['code'])) $code = $_GET['code']; else $code = '';

	switch ($task) {
		case 'check':
			if ($code == $_SESSION['CapchaSecurityCode']) return true;
			else return false;
		break;
		default:
		case 'getcaptcha':
			$w = 100;
			$h = 40;

			$img = imageCreate($w, $h);

			$cb = imageColorAllocate($img, 192, 192, 192);

			$cc = imageColorAllocate($img, 255, 0, 0);
			$ct = imageColorAllocate($img, 255, 255, 32);

			$cca = imageColorAllocateAlpha($img, 255, 0, 0, 64);
			$cta = imageColorAllocateAlpha($img, 255, 255, 32, 64);

			imageFilledRectangle($img, 0, 0, $w, $h, $cb);

			imageLine($img, mt_rand(0, $w), mt_rand(0, $h), mt_rand(0, $w), mt_rand(0,$h), $cca);
			imageLine($img, mt_rand(0, $w), mt_rand(0, $h), mt_rand(0, $w), mt_rand(0,$h), $cta);

			imageLine($img, mt_rand(0, $w), mt_rand(0, $h), mt_rand(0, $w), mt_rand(0, $h), $cca);
			imageLine($img, mt_rand(0, $w), mt_rand(0, $h), mt_rand(0, $w), mt_rand(0, $h), $cta);

			$generated_code = $_SESSION['CapchaSecurityCode'] = mt_rand(0,9999);

			imageString($img, 5, 7, 1, sprintf('%04d', mt_rand(0,9999)), $cc);
			imageString($img, 5, 57, 15, sprintf('%04d', $generated_code), $ct);

			header("Content-type: image/png");
			imagePNG($img);
		break;
	}
?>